package org.ruanjj.collection;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

public class TreeSetTest {

    public static void main(String[] args) {
        User u1 = new User("Emily",13);
        User u2 = new User("Tom",15);
        User u3 = new User("Jeffy",15);
        User u4 = new User("Emily",19);

        Comparator<User> comparator = new Comparator<User>() {
            @Override
            public int compare(User o1, User o2) {
                int value =  - (o1.getAge()- o2.getAge());

                if(value == 0){
                  return o1.getName().compareTo(o2.toString());
                }else{
                    return value;
                }
            }
        };

        TreeSet<User> userTreeSet= new TreeSet<>(comparator);
        userTreeSet.add(u1);
        userTreeSet.add(u2);
        userTreeSet.add(u3);
        userTreeSet.add(u4);

        Iterator<User> iterator = userTreeSet.iterator();

        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
}
